package com.csair.platform.cit.service.biz.impl;

import com.csair.invoice.common.base.util.collection.CollectionUtil;
import com.csair.invoice.common.base.util.date.DatetimeUtil;
import com.csair.platform.cit.facade.model.CitOverviewVo;
import com.csair.platform.cit.facade.model.CitQueryParam;
import com.csair.platform.cit.service.biz.OverviewService;
import com.csair.platform.cit.service.constant.CitDeclareStatusConstant;
import com.csair.platform.cit.service.dao.CitDeclareRecordRepository;
import com.csair.platform.cit.service.dao.a200000.CitNsrxxRepository;
import com.csair.platform.cit.service.entity.CitDeclareRecord;
import com.csair.platform.cit.service.entity.CitNsrxx;
import lombok.Data;
import lombok.experimental.Accessors;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.data.domain.Example;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;


import java.time.LocalDate;
import java.time.ZoneId;
import java.time.temporal.TemporalAdjusters;
import java.util.Date;
import java.util.List;

@Service
public class OverviewServiceImpl implements OverviewService {


    @Autowired
    private CitDeclareRecordRepository citDeclareRecordRepository;

    /**
     * 纳税人信息dao
     */
    @Autowired
    CitNsrxxRepository nsrxxDao;

    @Override
    public CitOverviewVo selectCitOverview(CitQueryParam citQueryParam) {

        //根据申报月份查询 税款署期范围
        Date declareDate = DatetimeUtil.strToDate(citQueryParam.getDeclareDate(), DatetimeUtil.MONTH_FORMAT);
        // atZone()方法返回在指定时区从此Instant生成的ZonedDateTime。
        LocalDate localDate = declareDate.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
        //获取上一个季度
        QuarterYearAndMonth quarterYearAndMonth = lastQuarterYearAndMonth(localDate.getYear(), localDate.getMonthValue());

        String minMonthValue = LocalDate.of(quarterYearAndMonth.getYearValue(), quarterYearAndMonth.getMinMonthValue(), 1).toString();

        LocalDate quarterMaxLocalDate = LocalDate.of(quarterYearAndMonth.getYearValue(), quarterYearAndMonth.getMaxMonthValue(), 1);
        String maxMonthValue = quarterMaxLocalDate.with(TemporalAdjusters.lastDayOfMonth()).toString();

        CitDeclareRecord  entity=new CitDeclareRecord();
        entity.setNsrsbh(citQueryParam.getNsrsbh());
        entity.setSkssqq(minMonthValue);
        entity.setSkssqz(maxMonthValue);
        List<CitDeclareRecord> citDeclareRecords =citDeclareRecordRepository.findAll(Example.of(entity));

        //获得必填数据
        CitNsrxx citNsrxx = nsrxxDao.findByTaxNoAndSkss(citQueryParam.getNsrsbh(), minMonthValue, maxMonthValue);

        CitOverviewVo citOverviewVo = new CitOverviewVo();
        citOverviewVo.setNsrsbh(CollectionUtils.isEmpty(citDeclareRecords) ? "" : citDeclareRecords.get(0).getNsrsbh())
                .setSkssqq(minMonthValue)
                .setSkssqz(maxMonthValue)
                .setTaxCalculationStatus(taxCalculationStatus(CollectionUtils.isEmpty(citDeclareRecords) ? null:citDeclareRecords.get(0), citNsrxx))
                .setTaxSbStatus(CitDeclareStatusConstant.citDeclareStatusMap.get(CollectionUtils.isEmpty(citDeclareRecords) ? "" : citDeclareRecords.get(0).getStatus()));
        return citOverviewVo;
    }


    /**
     * 根据月份获取上一个季度。
     *
     * @param monthValue
     * @return
     */
    private QuarterYearAndMonth lastQuarterYearAndMonth(int year, int monthValue) {
        QuarterYearAndMonth quarterYearAndMonth = new QuarterYearAndMonth();
        int lastQuarter;
        //如果查询月份小于3， 则查询去年的第四季度
        //返回去年的第四季度
        if (3 - monthValue >= 0) {
            quarterYearAndMonth.setYearValue(year - 1);
            lastQuarter = 4;
        } else {
            //如果月份大于3， 则查询未今年
            quarterYearAndMonth.setYearValue(year);
            lastQuarter = monthValue % 3 > 0 ? (monthValue / 3) : (monthValue / 3) - 1;
        }
        int quarterMaxMonthValue = lastQuarter * 3;
        int quarterMinMonthValue = quarterMaxMonthValue - 2;
        quarterYearAndMonth.setMinMonthValue(quarterMinMonthValue);
        quarterYearAndMonth.setMaxMonthValue(quarterMaxMonthValue);
        return quarterYearAndMonth;
    }


    /**
     * 获取资料填写状态
     *
     * @return
     */
    private String taxCalculationStatus(CitDeclareRecord citDeclareRecord, CitNsrxx citNsrxx) {
        //填写中
        if (citNsrxx != null && citDeclareRecord == null) {
            return CitDeclareStatusConstant.CIT_TAX_CALCULATION_STATUS_WRITING;
        }
        //未填写
        if (citNsrxx == null) {
            return CitDeclareStatusConstant.CIT_TAX_CALCULATION_STATUS_NOT;
        }
        //填写完成
        if (citDeclareRecord != null) {
            return CitDeclareStatusConstant.CIT_TAX_CALCULATION_STATUS_FINISH;
        }
        return null;
    }

    @Data
    @Accessors(chain = true)
    class QuarterYearAndMonth {
        //查询年份
        private int yearValue;

        //查询最小时间
        private int minMonthValue;

        //查询最大时间
        private int maxMonthValue;

    }

}
